package com.suredy.app.index.ctrl;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.suredy.Constants;
import com.suredy.core.ctrl.BaseFlowCtrl;
import com.suredy.security.model.User;
import com.suredy.security.service.UserSrv;
import com.suredy.tools.checkcode.SuredyLoginCheckImage;

import eabnp.basic.result.IResult;
import eabnp.eflow.client.impl.local.IEFlowClientRmi;
import eabnp.eflow.session.ESession;

@Controller
public class LoginCtrl extends BaseFlowCtrl {

	@Value("${SYS.SYSUN}")
	private String sysUn;
	
	@Autowired
	private SuredyLoginCheckImage suredyLoginCheckImage;

	@Autowired
	private UserSrv userSrv;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginIndex() {
		this.getSession();
		return "login";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout() {
		this.clearSession();
		return "redirect:login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(String userCode, String password, String checkCode, Boolean startupFlow, RedirectAttributes attributes) {
		if (!this.isTheSameCheckCode(checkCode)) {
			attributes.addFlashAttribute("err_msg", "校验码错误");
			attributes.addFlashAttribute("uniqueCode", userCode);

			return "redirect:login";
		}

		if (StringUtils.isBlank(userCode) || StringUtils.isBlank(password)) {
			attributes.addFlashAttribute("err_msg", "请正确填写用户名与密码");
			attributes.addFlashAttribute("uniqueCode", userCode);

			return "redirect:login";
		}

		User user = this.userSrv.getUser(userCode);

		if (user == null || !password.equals(user.getPassword())) {
			attributes.addFlashAttribute("err_msg", "用户名与密码不匹配");
			attributes.addFlashAttribute("uniqueCode", userCode);

			return "redirect:login";
		}
		
		this.setUser(user);

		if (startupFlow) {
			IEFlowClientRmi client = getEFlowClient();

			if (client == null) {
				attributes.addFlashAttribute("err_msg", "登录失败！无法获取流程信息!");

				return "redirect:login";
			}

			try {
				IResult result = client.Exec("getSysCtrlInfo", new Object[] {user.getUserCode(),sysUn});

				if (result.isSuccess()) {
					setUser(user);
					setESession((ESession) result.getResultObject());
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}

		return "redirect:index";
	}

	@RequestMapping("/check-img/{width}/{height}")
	public void checkImg(HttpServletResponse response, @PathVariable int width, @PathVariable int height) {
		try {
			String code = this.suredyLoginCheckImage.write(response.getOutputStream(), width, height);

			this.setSessionAttribute(Constants.SESSION_LOGIN_CHECK_CODE, code);
		} catch (IOException e) {
		}
	}

}
